package cn.iocoder.yudao.module.promotion.newcustomer.service.admin;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.PageService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.api.promotion.newcustomer.vo.NewCustomerConfigVO;
import cn.iocoder.yudao.module.promotion.newcustomer.convert.NewcustomerConfigConvert;
import cn.iocoder.yudao.module.promotion.newcustomer.dao.NewcustomerConfigMapper;
import cn.iocoder.yudao.module.promotion.newcustomer.pojo.dto.NewcustomerConfigQueryDTO;
import cn.iocoder.yudao.module.promotion.newcustomer.pojo.dto.NewcustomerConfigSaveDTO;
import cn.iocoder.yudao.module.promotion.newcustomer.pojo.po.NewcustomerConfig;
import cn.iocoder.yudao.module.promotion.newcustomer.service.core.CoreNewcustomerConfigService;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/07/11
 */
@Service
public class NewcustomerConfigService extends BaseService<NewcustomerConfig, NewCustomerConfigVO, NewcustomerConfigQueryDTO> implements PageService<NewCustomerConfigVO, NewcustomerConfigQueryDTO> {
    @Resource
    private NewcustomerConfigMapper newcustomerConfigMapper;

    @Resource
    private CoreNewcustomerConfigService coreNewcustomerConfigService;

    @Override
    public NewcustomerConfigMapper getInjectedMapper() {
        return newcustomerConfigMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<NewcustomerConfig> buildQueryWrapper(NewcustomerConfigQueryDTO param) {
        CoreQueryWrapper<NewcustomerConfig> wrapper = super.buildQueryWrapper(param);
        wrapper.eqIfPresent("enabled", param.getEnabled());
        return wrapper;
    }

    @Override
    public NewCustomerConfigVO convertToViewObject(NewcustomerConfig entity) {
        return NewcustomerConfigConvert.INSTANCE.toViewObject(entity);
    }


    @DataPermission(enable = false)
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(Long shopId, NewcustomerConfigSaveDTO param) {
        NewcustomerConfig entity = NewcustomerConfigConvert.INSTANCE.toEntity(param);
        entity.setShopId(shopId);
        NewcustomerConfig config = coreNewcustomerConfigService.getOrCreate(shopId);
        entity.setId(config.getId());
        return newcustomerConfigMapper.updateById(entity) > 0;
    }

}
